package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.CategoryModel;

import admin.data.CategoryData;

public class UpdateCategoryServlet extends HttpServlet{
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException {
		
		String strIds = (String) request.getParameter("ids");
        String action = (String) request.getParameter("action");
        int status = 0;
        String[] ids = strIds.split(",");
        CategoryData cData = new CategoryData();
        
        ArrayList<String> statusAction = new ArrayList<String>(Arrays.asList("active", "pending", "inactive"));
        
        for (int i = 0; i < ids.length; i++) {
            if (!ids[i].isEmpty()) {
            	if(statusAction.contains(action.toLowerCase())) {
	                if (action.equalsIgnoreCase("active")) {
	                	status = 2;
	                } else if (action.equalsIgnoreCase("pending")) {
						status = 1;
					} else if (action.equalsIgnoreCase("inactive")) {
						status = 0;
					}
	                
	                CategoryModel category = cData.getCategory(new Long(ids[i]));
	                if (category != null) {
	                	category.setStatus(status);
	        			cData.update(category);
	        		}
            	} else if (action.equalsIgnoreCase("remove")) {
            		CategoryModel category = cData.getCategory(new Long(ids[i]));
					cData.delete(category);
				}
            }
        }
	}
}
